<?php
// 系统数据模型，主要是一些逻辑上比较复杂的实现后台系统功能的数据模型
class Role_model extends CI_Model {

    public function __construct()
    {
        parent::__construct();
        $this->load->database();
    }

    public function get_role_list()
    {
        $roleId = $this->session->userdata('roleId');
        $this->db->query("set max_sp_recursion_depth=12");
        return $this->db->query("call showRoleChildList({$roleId});")->result_array();
    }

    /** 获取当前角色下所有的权限，便于给次级角色配置权限又不会越级配置，如果是默认角色既不会展示
     * @param $roleId 角色ID
     * @return array 返回二维数组
     */
    public function get_power_data($roleId)
    {
        $powerData = array();
        $this->load->library('lw_db',array('tb_name'=>TB_METHOD),'tb_method');
        $rowsController = $this->tb_method->get_all('*',array('parent_id'=>0,'is_default'=>0),'sort','asc');
        $rowRole = $this->tb_role->get_one(array('id'=>$roleId));
        $rowParentRole = $this->tb_role->get_one(array('id'=>$rowRole['parent_id']));
        foreach($rowsController as $controller) {
            $powerController = array('id' => $controller['id'], 'text' => $controller['describe'] . '(' . $controller['name'] . ')');
            if ($rowParentRole['parent_id']==0) {
                $sql = "SELECT
                            m.*, rp.id AS power_id
                        FROM
                            tb_method m
                        LEFT JOIN tb_role_power rp ON m.id = rp.method_id AND rp.role_id = {$roleId}
                        where m.parent_id = {$controller['id']} AND m.is_default = 0
                        ORDER BY sort asc";
            } else {
                // 只列出当前角色的权限列表
                $parentRoleId = $rowParentRole['id'];
                $sql = "SELECT
                            m.*,rp2.id as power_id
                        FROM
                            tb_method m
                        LEFT JOIN tb_role_power rp1 on rp1.method_id = m.id
                        LEFT JOIN tb_role_power rp2 on rp2.method_id = m.id AND rp2.role_id = {$roleId}
                        WHERE m.parent_id = {$controller['id']} AND rp1.role_id = {$parentRoleId} AND m.is_default = 0";
            }
            $rowsMethod = $this->db->query($sql)->result_array();
            if ($rowsMethod) {
                $powerController['children'] = array();
                foreach ($rowsMethod as $method) {
                    $powerMethod = array('id' => $method['id'], 'text' => $method['describe'] . '(' . $method['name'] . ')');
                    if ($method['power_id']) {
                        $powerMethod['state'] = array('selected' => true);
                    }
                    array_push($powerController['children'], $powerMethod);
                }
            }
            array_push($powerData, $powerController);
        }
        $data = array();
        for($i=0;$i<sizeof($powerData);$i++){
            if(isset($powerData[$i]['children'])){
                array_push($data,$powerData[$i]);
            }
        }

        return $data;
    }

}